package day02am;

import java.io.*;
import java.net.Socket;

/**
 * @author JokerQ
 * @date 2020/8/23 - 18:30
 */
public class Client_v2 {
    public static void main(String[] args) throws IOException {
        /*
        1、服务端的地址信息（IP）
        2、服务端打开的端口
         */
        Socket socket = new Socket("127.0.0.1", 8888);
        System.out.println("客户端向服务端发起请求...");
        System.out.println("客户端LocalAddress:"+socket.getLocalAddress());
        System.out.println("客户端LocalPort:"+socket.getLocalPort());
        System.out.println("服务端InetAddress:"+socket.getInetAddress());
        System.out.println("服务端Port:"+socket.getPort());

        //将字节写入此套接字的输出流
        OutputStream out = socket.getOutputStream();
        //InputStream is = socket.getInputStream();
        //BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"));
        //字符转换为字节，可使用指定的 charset 将要写入流中的字符编码成字节。
        OutputStreamWriter osw = new OutputStreamWriter(out, "utf-8");
        //创建一个使用默认大小输出缓冲区的缓冲字符输出流
        BufferedWriter bw = new BufferedWriter(osw);
        //按行写字符串，自动行刷新
        PrintWriter writer = new PrintWriter(bw,true);
        writer.println("你好呀！！！");
        //String str = reader.readLine();
        //System.out.println("收到服务端回复："+str);
        socket.close();

        /*
        只能连接一次，连接成功后服务端停止，客户端再次连接报错
        Exception in thread "main" java.net.ConnectException: Connection refused: connect
         */
    }
}
